﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib;

namespace GanttPlannerDemo
{
    public partial class Form3 : Form
    {
        List<AppointmentGroup> apps = new List<AppointmentGroup>();

        public Form3()
        {
            for (int i = 11; i < 121; i++)
            {
                AppointmentGroup ag = new AppointmentGroup();
                ag.GroupTitle = string.Format("The Group {0}", i);
                ag.GroupId = i;
                if (i == 12)
                {
                    Appointment ap = new Appointment()
                    {
                        Title = "Test" + i.ToString(),
                        GroupId = i,
                        StartTime = DateTime.Today.AddHours(14).AddMinutes(40),
                        EndTime = DateTime.Today.AddHours(16).AddMinutes(15),
                        Percent = 0.7f,
                        Tooltip = "Test" + i.ToString() + "\n\r" + DateTime.Today.AddHours(14).AddMinutes(40).ToShortTimeString()
                    };
                    ag.Add(ap);

                    Appointment ap2 = new Appointment()
                    {
                        Title = "Test2" + i.ToString(),
                        GroupId = i,
                        StartTime = DateTime.Today.AddHours(14).AddMinutes(15),
                        EndTime = DateTime.Today.AddHours(15).AddMinutes(15),
                        Percent = 0.2f
                    };
                    ag.Add(ap2);

                    Appointment ap3 = new Appointment()
                    {
                        Title = "Test2" + i.ToString(),
                        GroupId = i,
                        StartTime = DateTime.Today.AddHours(14).AddMinutes(15),
                        EndTime = DateTime.Today.AddHours(15).AddMinutes(49),
                        Percent = 0.2f
                    };
                    ag.Add(ap3);

                    Appointment ap4 = new Appointment()
                    {
                        Title = "Test2" + i.ToString(),
                        GroupId = i,
                        StartTime = DateTime.Today.AddHours(14).AddMinutes(15),
                        EndTime = DateTime.Today.AddHours(17).AddMinutes(49),
                        Percent = 0.2f
                    };
                    ag.Add(ap4);

                    Appointment ap5 = new Appointment()
                    {
                        Title = "Test2" + i.ToString(),
                        GroupId = i,
                        StartTime = DateTime.Today.AddHours(14).AddMinutes(15),
                        EndTime = DateTime.Today.AddHours(17).AddMinutes(100),
                        Percent = 0.2f
                    };
                    ag.Add(ap5);

                    ag.WorkTimes.Add(new TimeRange() { startTime = DateTime.Today.AddHours(9), endTime = DateTime.Today.AddHours(15) });
                    ag.WorkTimes.Add(new TimeRange() { startTime = DateTime.Today.AddHours(17), endTime = DateTime.Today.AddHours(18) });
                }

                apps.Add(ag);
            }
            DateTime dt = DateTime.Today.Date;

            AppointmentGroup ag1 = new AppointmentGroup();
            apps.Add(ag1);

            InitializeComponent();
            this.ganttPlanner1.StartTime = dt;
        }

        private void tbTotalHourCount_Scroll(object sender, EventArgs e)
        {
            this.ganttPlanner1.TotalHour = this.tbTotalHourCount.Value;
            this.lblHourCount.Text = string.Format("Total Hours {0}", this.tbTotalHourCount.Value);
        }

        private void tbScaleWidth_Scroll(object sender, EventArgs e)
        {
            this.ganttPlanner1.HalfHourWidth = this.tbScaleWidth.Value;
            this.lblScaleWidth.Text = string.Format("Half Hour Width {0}", this.tbScaleWidth.Value);
        }

        private void ganttPlanner1_ResolveAppointments(object sender, ResolveAppointmentsEventArgs args)
        {
            args.AppointmentGroups = apps;
        }

        private void tbShowingLines_Scroll(object sender, EventArgs e)
        {
            this.ganttPlanner1.RowPageSize = this.tbShowingLines.Value;
            this.lblShowingLines.Text = string.Format("Showing Lines {0}", this.tbShowingLines.Value);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Appointment ap = new Appointment()
            {
                Title = "NewNew",
                GroupId = 3,
                StartTime = DateTime.Today.AddHours(24).AddMinutes(40),
                EndTime = DateTime.Today.AddHours(26).AddMinutes(15)
            };

            ap.Locked = true;
            apps[3].Add(ap);

            ganttPlanner1.Invalidate();
        }

        private void ganttPlanner1_AppoinmentMove(object sender, AppointmentEventArgs e)
        {
            //MessageBox.Show(e.Appointment.Id);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Appointment m_App = new Appointment();
            m_App.StartTime = ganttPlanner1.SelectionStart;
            m_App.EndTime = ganttPlanner1.SelectionEnd;
            m_App.BorderColor = Color.Red;
            m_App.GroupId = ganttPlanner1.SelectionGroup;
            apps.Where(ag => ag.GroupId == 11).First().Add(m_App);

            ganttPlanner1.Invalidate();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            cbDrag.DataSource = Enum.GetNames(typeof(GanttPlanner.DragType));

            cbResize.DataSource = Enum.GetNames(typeof(GanttPlanner.ResizeType));

            cbDrag.SelectedText = ganttPlanner1.AllowDragStatus.ToString();
            cbResize.SelectedText = ganttPlanner1.AllowResizeStatus.ToString();
        }

        private void cbDrag_SelectedIndexChanged(object sender, EventArgs e)
        {
            ganttPlanner1.AllowDragStatus = (GanttPlanner.DragType)Enum.Parse(typeof(GanttPlanner.DragType), cbDrag.Text);
        }

        private void cbResize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ganttPlanner1.AllowResizeStatus = (GanttPlanner.ResizeType)Enum.Parse(typeof(GanttPlanner.ResizeType), cbResize.Text);
        }

        private void ganttPlanner1_NewAppointment(object sender, NewAppointmentEventArgs e)
        {
            MessageBox.Show("New Appointment Created :" + e.Title);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ganttPlanner1.AllowInplaceEditing = this.checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (ganttPlanner1.SelectedAppointment != null)
                ganttPlanner1.SelectedAppointment.Locked = this.checkBox2.Checked;
            ganttPlanner1.Invalidate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (ganttPlanner1.SelectedAppointment != null)
            {
                colorDialog1.Color = ganttPlanner1.SelectedAppointment.BorderColor;

                if (colorDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    ganttPlanner1.SelectedAppointment.BorderColor = colorDialog1.Color;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (ganttPlanner1.SelectedAppointment != null)
            {
                colorDialog1.Color = ganttPlanner1.SelectedAppointment.Color;

                if (colorDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    ganttPlanner1.SelectedAppointment.Color = colorDialog1.Color;
                }
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            this.ganttPlanner1.EnableTooltip = this.checkBox3.Checked;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            this.ganttPlanner1.ShowPercent = this.checkBox4.Checked;
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            this.ganttPlanner1.ShowNowLine = this.checkBox5.Checked;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ganttPlanner1.LocateToRow((int)this.numericUpDown1.Value);

            ganttPlanner1.SelectedGroupId = (int)this.numericUpDown1.Value + 1;
        }

        #region Drag Relations.

        private int originalGroupId;

        private void ganttPlanner1_SelectionChanged(object sender, EventArgs e)
        {
            if (ganttPlanner1.SelectedAppointment != null)
            {
                originalGroupId = ganttPlanner1.SelectedAppointment.GroupId;
            }
            else
            {
                originalGroupId = -1;
            }

            #region show selection info.

            this.label2.Text = string.Format("Selected Area: Group {0} , {1} to {2}", this.ganttPlanner1.SelectionGroup, ganttPlanner1.SelectionStart.ToShortTimeString(), ganttPlanner1.SelectionEnd.ToShortTimeString());
            if (ganttPlanner1.SelectedAppointment != null)
            {
                label1.Text = string.Format("Selected Node：{0}", this.ganttPlanner1.SelectedAppointment.Title);
                this.checkBox2.Checked = this.ganttPlanner1.SelectedAppointment.Locked;
            }

            #endregion
        }

        private void ganttPlanner1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.originalGroupId != -1 && this.ganttPlanner1.SelectedAppointment.GroupId != this.originalGroupId)
            {
                MessageBox.Show(string.Format("Node Id：{0} has moved from: group {1} to: group {2}", ganttPlanner1.SelectedAppointment.Id, originalGroupId, ganttPlanner1.SelectedAppointment.GroupId));
            }
        }

        #endregion

        #region Drag In

        bool isDrag;

        private void Form3_MouseUp(object sender, MouseEventArgs e)
        {
            Point p = e.Location;
            //if (p.Y>200)
            //{
            isDrag = true;
            //}
        }

        private void ganttPlanner1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrag)
            {
                int groupId;

                this.ganttPlanner1.GetTimeAt(e.X, e.Y, out groupId);
                MessageBox.Show(string.Format("Current X:{0} Y:{1} GroupId:{2}", e.X, e.Y, groupId));

                isDrag = false;
            }
            this.Text = string.Format("Mouse Point time：{0}", ganttPlanner1.GetTimeAt(e.X, e.Y).ToString());
        }

        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                isDrag = false;
            }
        }

        #endregion

        private void button6_Click(object sender, EventArgs e)
        {
            if (ganttPlanner1.SelectedAppointment!=null)
            {
                ganttPlanner1.LocateToTime(ganttPlanner1.SelectedAppointment.StartTime);
            }
        }
    }
}
